package com.wc.AlgoOJ.LQ1341;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/19 18:11
 * @description 好数之和
 * http://43.138.190.70:8888/p/LQ1341
 */

public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 11;
    // f[i] 表示 1 ~ i个9中满足条件的好数之和的值
    static long[] f = new long[N];
    // f1[i] 表示1 ~ i个9中满足条件的个数
    static long[] f1 = new long[N];


    static long[] base = new long[N];
    static int[] nums = new int[N];

    // f2[i][j] 表示 第i位以j开头的好数的和
    static long[][] f2 = new long[N][10];

    // f2[i][j] 表示 第i位以j开头的好数的个数
    static int[][] f3 = new int[N][10];

    static int l, r;

    static void init() {
        base[0] = 1;
        for (int i = 1; i < N; i++) base[i] = base[i - 1] * 10;
//        f[4] = 2022;
        for (int i = 4; i < N; i++) {
            int move = i - 3;
            for (int j = 0; j < move; j++) {
                long l = base[j];
                long r = base[i - 4 - j];
                long ll = base[4] * r;
                f[i] += (l - 1) * l * ll / 2 * r + l * 2022 * r * r + l * (r - 1) * r / 2;
                f1[i] += l * r;
            }
            // 去重
            if (i >= 7) {
                // 2022022 移动
                move = i - 6;
                for (int j = 0; j < move; j++) {
                    long l = base[j];
                    long r = base[i - j - 7];
                    long ll = base[7] * r;
                    f[i] -= (l - 1) * l * ll / 2 * r + l * 2022022 * r * r + l * (r - 1) * r / 2;
                    f1[i] -= l * r;
                }
                // 20222022
                move = i - 7;
                for (int j = 0; j < move; j++) {
                    long l = base[j];
                    long r = base[i - j - 8];
                    long ll = base[8] * r;
                    f[i] -= (l - 1) * l * ll / 2 * r + l * 20222022 * r * r + l * (r - 1) * r / 2;
                    f1[i] -= l * r;
                }

                // 202202022 已经不想写了
                if (i == 9) {
                    int repeat = 202202022;
                    for (int j = 0; j < 10; j++) {
                        f[i] -= repeat;
                        repeat += base[5];
                    }
                    f1[i] -= 10;
                }
            }
        }


//        f2[5][0] =  2022
//        f2[5][1] = 12022;
//        f2[5][2] = 22022 + 20220 + 20221 + 20222 + 20223 + 20224 + 20225 + 20226 + 20227 + 20228 + 20229;
//        f2[5][3] = 32022;
//        f2[5][4] = 42022;
//        f2[5][5] = 52022;
//        f2[5][6] = 62022;
//        f2[5][7] = 72022;
//        f2[5][8] = 82022;
//        f2[5][9] = 92022;

//        f2[6][0] =  012022 + 012022 + 022022 + ... + 092022     020220   020221         020229
//        f2[6][1] = (102022 + 112022 + 122022 + ... + 192022) + (120220 + 120221 + ... + 120229) 20
//        f2[6][2] = (202022 + 212022 + 222022 + ... + 292022) + (220220 + 220221 + ... + 220229) + (202200 + 202201 + ... + 202299)
//        f2[6][3] = (302022 + 312022 + 322022 + ... + 392022) + (320220 + 320221 + ... + 320229)
//        f2[6][4] = (402022 + 412022 + 422022 + ... + 492022) + (420220 + 420221 + ... + 420229)

        f2[4][2] = 2022;
        f3[4][2] = 1;
        for (int i = 5; i < N; i++) {
            for (int j = 0; j <= 9; j++) {
                if (j == 0) {
                    for (int k = 0; k <= 9; k++) {
                        f2[i][j] += f2[i - 1][k];
                        f3[i][j] += f3[i - 1][k];
                    }
                } else {
                    f2[i][j] = f2[i][j - 1] + j * base[i - 1] * f3[i][j - 1];
                    f3[i][j] = f3[i][j - 1];
                }
                // 他为2的时候特判断
                if (j == 2) {


                }
            }
        }
        for (int i = 5; i < N; i++) {
            long r = base[i - 4];
            f2[i][2] += 2022 * r * r + (r - 1) * r / 2;
            f3[i][2] += r;
            for (int j = 0; j <= 9 && i + 1 < N; j++) {
                f2[i + 1][j] += f2[i][2];
                f3[i + 1][j] += f3[i][2];
            }
            // 还需要去重
        }
    }
    // 266791008107715

    public static void main(String[] args) {
        init();
//        l = sc.nextInt();
//        r = sc.nextInt();
        r = 30000;
        test(1, r);
        out.println(dp(r));
        out.flush();
    }

    static long dp(int n) {
        if (n < 2022) return 0;
        int len = 0;
        int baseN = n;
        while (n > 0) {
            nums[len++] = n % 10;
            n /= 10;
        }
        long res = 0;
        int last = 0;
        for (int i = len - 1; i >= 0; i--) {
            int x = nums[i];
            if (x > 0) {
//                res += x * f[i] + last * base[i + 1] * f1[i] + (long) (x - 1) * x / 2L * base[i] * f1[i];

            }

            last = last * 10 + x;
            if (String.valueOf(last).contains("2022")) {

            }
            if (i == 0 && String.valueOf(baseN).contains("2022")) res += last;
        }

        for (int i = 1; i < len; i++) {
            for (int j = 1; j <= 9; j++) {
                res += f2[i][j];
            }
        }
        return res;
    }

    static void test(int l, int r) {
        long res = 0;
        int cnt = 0;
        for (int i = l; i <= r; i++) {
            if (String.valueOf(i).contains("2022")) {
                res += i;
                cnt++;
            }
        }
        System.out.println(res);
        System.out.println();
    }

}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
